﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Common.Tool.Models
{
    public class RichDocument
    {
        public bool isSubscribed = false;
        public ObservableCollection<string> Text { get; set; } = new ObservableCollection<string>();
        public List<System.Windows.Media.Brush> Brush { get; set; } = new List<Brush>() { };
        public void Clear()
        {
            Text.Clear(); 
            Brush.Clear();
        }
        /// <summary>
        /// 插入文本
        /// </summary>
        public void AppendText(System.Windows.Media.Brush brush, string temp, string msg, DateTime? time = null)
        {
            try
            {
                lock (this)
                {
                    //添加文本
                    Brush.Add(brush);
                    Text.Add((time ?? DateTime.Now).ToString("HH:mm:ss.fff") + $" [{temp}] " + msg.Replace("\r", "\\r").Replace("\n", "\\n"));

                    //todo 验证
                    if (Text.Count > 10000)
                    {
                        Text.Remove(Text.First());
                        Brush.Remove(Brush.First());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入一级Title
        /// </summary>
        public void AppendTile1(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.RoyalBlue, temp, msg, time);
        }
        /// <summary>
        /// 插入二级Title
        /// </summary>
        public void AppendTile2(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.DodgerBlue, temp, msg, time);
        }
        /// <summary>
        /// 插入信息
        /// </summary>
        public void AppendInfo(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.Black, temp, msg, time);
        }
        /// <summary>
        /// 插入通信
        /// </summary>
        public void AppendCommunication(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.DimGray, temp, msg, time);
        }
        /// <summary>
        /// 插入提示
        /// </summary>
        public void AppendTip(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.OrangeRed, temp, msg, time);
        }
        /// <summary>
        /// 插入结果
        /// </summary>
        public void AppendResult(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.DeepSkyBlue, temp, msg, time);
        }
        /// <summary>
        /// 插入错误
        /// </summary>
        public void AppendErr(string temp, string msg, DateTime? time = null)
        {
            AppendText(Brushes.Tomato, temp, msg, time);
        }
    }
}
